package com.tuanbiao.presenter.setting;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.hzjq.data.entity.ZbServiceEntity;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.SettingController;
import com.tuanbiao.data.callback.OnBaseResultCallback;
import com.tuanbiao.data.entity.ZBServiceItem;
import com.tuanbiao.utils.LibCollections;

import java.util.ArrayList;
import java.util.List;

import timber.log.Timber;

/**
 * @author abiao
 * @date 2022/10/26 15:41
 * @description:
 */
public class ZbServerChangePresenter extends BasePresenter<SettingController.Model, SettingController.ZbServerChangeView> implements SettingController.ZbServerChangePresenter {
    private List<ZBServiceItem> zbServiceItems;
    private ZbServiceEntity selectEntity;
    boolean isUse;

    public ZbServerChangePresenter(SettingController.Model model, SettingController.ZbServerChangeView rootView) {
        super(model, rootView);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private void queryZbIp() {
        mModel.queryZbIp(zbServiceEntities -> {
            if (null == zbServiceItems) {
                zbServiceItems = new ArrayList<>();
                for (ZbServiceEntity entity : zbServiceEntities) {
                    zbServiceItems.add(new ZBServiceItem(entity.getId(), entity.getIp(), entity.getName(), entity.getPort()));
                }
            }
        });
        refreshService();
    }

    private void refreshService() {
        selectEntity = mModel.getSettingParams().getZbIp();
        Timber.i("refreshService#" + selectEntity);
        mRootView.showServices(convert(selectEntity));
    }

    @Override
    public void setNewZbIp(List<ZbServiceEntity> data) {
        mModel.setNewZbIp(data, aBoolean -> {
            if (aBoolean) {
                mRootView.showMessage("设置新爆破记录服务器成功");
                refreshService();
            }
            else mRootView.showMessage("设置新爆破记录服务器失败");
        });
    }

    @Override
    public void insertZbService(ZBServiceItem entity) {
        mModel.insertZbIp(convert(entity), aBoolean -> {
            if (aBoolean) {
                mRootView.showMessage("新增爆破记录服务器成功");
                refreshService();
            }
            else mRootView.showMessage("新增爆破记录服务器失败");
        });
    }

    @Override
    public void selectService(ZBServiceItem entity) {
        ZbServiceEntity zbService = new ZbServiceEntity();
        zbService.setId(entity.getId());
        zbService.setIp(entity.getIp());
        zbService.setName(entity.getName());
        zbService.setPort(entity.getPort());
        selectEntity = zbService;
        mModel.selectZbIp(zbService, aBoolean -> {
            if (aBoolean) mRootView.showMessage("选择爆破记录服务器成功");
            else mRootView.showMessage("选择爆破记录服务器失败");
        });
    }

    @Override
    public void showServiceSelect() {
        if (zbServiceItems == null) {
            mModel.queryZbIp(zbServiceEntities -> {
                if (null == zbServiceItems) {
                    zbServiceItems = new ArrayList<>();
                    for (ZbServiceEntity entity : zbServiceEntities) {
                        zbServiceItems.add(new ZBServiceItem(entity.getId(), entity.getIp(), entity.getName(), entity.getPort()));
                    }
                }
                showServiceSelectDialog();
            });
        } else {
            showServiceSelectDialog();
        }

    }

    @Override
    public void updateZbService(ZBServiceItem zbServiceItem) {
        mModel.updateZbIp(convert(zbServiceItem), aBoolean -> {
            if (aBoolean) {
                mRootView.showMessage("修改爆破记录服务器成功");
                refreshService();
            }
            else mRootView.showMessage("修改爆破记录服务器失败");
        });
    }

    @Override
    public void removeZBIp() {
        Timber.i("toggleUseState#" + selectEntity);
        mModel.removeZBIp();
        refreshService();
    }

    private ZbServiceEntity convert(ZBServiceItem zbServiceItem) {
        if (null == zbServiceItem) return null;
        ZbServiceEntity entity = new ZbServiceEntity();
        entity.setId(zbServiceItem.getId());
        entity.setName(zbServiceItem.getName());
        entity.setIp(zbServiceItem.getIp());
        entity.setPort(zbServiceItem.getPort());
        return entity;
    }

    private ZBServiceItem convert(ZbServiceEntity zbServiceItem) {
        if (null == zbServiceItem) return null;
        ZBServiceItem entity = new ZBServiceItem();
        entity.setId(zbServiceItem.getId());
        entity.setName(zbServiceItem.getName());
        entity.setIp(zbServiceItem.getIp());
        entity.setPort(zbServiceItem.getPort());
        return entity;
    }

    private void showServiceSelectDialog() {
        mRootView.showSelectDialog(zbServiceItems, selectEntity == null ? 0 : createIndex());
    }

    private int createIndex() {
        for (int i = 0; i < LibCollections.size(zbServiceItems); i++) {
            if (zbServiceItems.get(i).getId() == selectEntity.getId()) {
                return i;
            }
        }
        return 0;
    }
}
